define(['vue','axios','manager-box','leader-box','student-box','jquery', 'bootstrap', 'backend', 'table', 'form'], function (Vue,Axios,ManagerBox,LeaderBox,StudentBox, $, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'classes/classes/index',
                    add_url: 'classes/classes/add',
                    edit_url: 'classes/classes/edit',
                    multi_url: 'classes/classes/multi',
                    del_url: 'classes/classes/del',
                    table: 'classes',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                queryParams: function (params) {
                    params.rel_type = Config.relType;
                    params.rel_id = Config.relId;
                    return params;
                },
                escape: false,
                pk: 'id',
                sortName: 'weigh desc,id desc', //要使排序生效，在前面添加 weigh desc, 并且数据表weigh字段不能全为0,controller里$list=...->order前再添加->order("weigh desc")
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: '班级名称'},
                        {field: 'rel_id', title: '学校'},
                        {field: 'addresslv_id', title: '地址标签',align:'left'},
                        {field: 'cover', title: '班级头像',formatter:Table.api.formatter.image},
                        {field: 'student_id', title: '班长'},
                        {field: 'manager_id', title: '管理员'},
                        {field: 'num', title: '班级人数'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    classname: 'btn btn-xs btn-danger btn-click',
                                    icon: 'fa fa-user-graduate',
                                    click: function (value, row, index) {
                                        Backend.api.open('/classes/student?cid='+row.id+'&ref=addtabs', '学生', {
                                            max: true,
                                            area: ['80%', '80%'],
                                            //offset: ['0px', '0px'],
                                            callback: function (data) {
                                                Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "学生"});
                                            }
                                        });
                                    }
                                }, {
                                    name: 'warning',
                                    classname: 'btn btn-xs btn-success btn-click',
                                    icon: 'fa fa-user-tie',
                                    click: function (value, row, index) {
                                        Backend.api.open('/classes/leader?cid='+row.id+'&ref=addtabs', '班长', {
                                            max: true,
                                            area: ['80%', '80%'],
                                            //offset: ['0px', '0px'],
                                            callback: function (data) {
                                                Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "班长"});
                                            }
                                        });
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });
            $(document).on('fa.event.success', function(event, data) {
                setTimeout(function() {
                    location.reload();
                }, 1000);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Vue.component('manager-box', ManagerBox);
            Vue.component('leader-box', LeaderBox);
            new Vue({
                el: '#app',
                components: {
                },
                data: {
                    leftOptions: Config.leftOptions,
                    rightOptions: Config.rightOptions
                }
            });
            $(document).ready(function(){
                // 获取当前年份和月份
                var currentYear = new Date().getFullYear();
                var currentMonth = new Date().getMonth() + 1; // getMonth() 返回0-11，所以加1得到1-12

                // 定义一个函数来确定当前的季节
                function getCurrentSeason(month) {
                    if (month >= 3 && month <= 5) return "春季";
                    if (month >= 6 && month <= 8) return "夏季";
                    if (month >= 9 && month <= 11) return "秋季";
                    return "冬季"; // 12, 1, 2月
                }

                // 检查#c-nianji元素是否存在
                var $nianjiElement = $('#c-nianji');
                if ($nianjiElement.length) { // 如果元素存在
                    // 检查#c-nianji的值是否为空
                    if($nianjiElement.val().trim() === '') {
                        // 设置为当前年份+"级"
                        $nianjiElement.val(currentYear + '级');
                    }
                } else {
                    console.log('未找到ID为"c-nianji"的元素');
                }

                // 检查#c-xueqi元素是否存在
                var $xueqiElement = $('#c-xueqi');
                if ($xueqiElement.length) { // 如果元素存在
                    // 检查#c-xueqi的值是否为空
                    if($xueqiElement.val().trim() === '') {
                        // 设置为当前季节
                        var season = getCurrentSeason(currentMonth);
                        $xueqiElement.val(season);
                    }
                } else {
                    console.log('未找到ID为"c-xueqi"的元素');
                }

                $(document).on('click dblclick', '#c-name', function(e) {
                    var nianji = $("#c-nianji").val();
                    var xueyuan ="";
                    if(!/请/.test($("#c-school_id_tree option:selected").text())) {
                        xueyuan = $("#c-school_id_tree option:selected").text().replace(/\s|&nbsp;|├|└/g, '');
                    }
                    var xueqi = $("#c-xueqi").val();
                    var qishu = $("#c-qishu").val();
                    var subject ="";
                    if(!/请/.test($("#c-subject_lv option:selected").text())) {
                        subject = $("#c-subject_lv option:selected").text().replace(/\s|&nbsp;|├|└/g, '');
                    }
                    if(e.type=="click"&&$("#c-name").val()==""){
                        $("#c-name").val(nianji+xueyuan+xueqi+subject+"第"+qishu+"期"+"1班");
                    }else if(e.type=="dblclick"){
                        $("#c-name").val(nianji+xueyuan+xueqi+subject+"第"+qishu+"期"+"1班");
                    }
                });
            });
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/add.js';
            document.head.appendChild(script);
            Controller.api.bindevent();
        },
        edit: function () {
            //Vue.component('manager-box', ManagerBox);
            Vue.component('leader_box', LeaderBox);
            new Vue({
                el: '#app',
                components: {
                },
                data: {
                    leftOptions: Config.leftOptions,
                    rightOptions: Config.rightOptions
                }
            });
            $(document).ready(function(){
                // 获取当前年份和月份
                var currentYear = new Date().getFullYear();
                var currentMonth = new Date().getMonth() + 1; // getMonth() 返回0-11，所以加1得到1-12

                // 定义一个函数来确定当前的季节
                function getCurrentSeason(month) {
                    if (month >= 3 && month <= 5) return "春季";
                    if (month >= 6 && month <= 8) return "夏季";
                    if (month >= 9 && month <= 11) return "秋季";
                    return "冬季"; // 12, 1, 2月
                }

                // 检查#c-nianji元素是否存在
                var $nianjiElement = $('#c-nianji');
                if ($nianjiElement.length) { // 如果元素存在
                    // 检查#c-nianji的值是否为空
                    if($nianjiElement.val().trim() === '') {
                        // 设置为当前年份+"级"
                        $nianjiElement.val(currentYear + '级');
                    }
                } else {
                    console.log('未找到ID为"c-nianji"的元素');
                }

                // 检查#c-xueqi元素是否存在
                var $xueqiElement = $('#c-xueqi');
                if ($xueqiElement.length) { // 如果元素存在
                    // 检查#c-xueqi的值是否为空
                    if($xueqiElement.val().trim() === '') {
                        // 设置为当前季节
                        var season = getCurrentSeason(currentMonth);
                        $xueqiElement.val(season);
                    }
                } else {
                    console.log('未找到ID为"c-xueqi"的元素');
                }

                $(document).on('click dblclick', '#c-name', function(e) {
                    var nianji = $("#c-nianji").val();
                    var xueyuan ="";
                    if(!/请/.test($("#c-school_id_tree option:selected").text())) {
                        xueyuan = $("#c-school_id_tree option:selected").text().replace(/\s|&nbsp;|├|└/g, '');
                    }
                    var xueqi = $("#c-xueqi").val();
                    var qishu = $("#c-qishu").val();
                    var subject ="";
                    if(!/请/.test($("#c-subject_lv option:selected").text())) {
                        subject = $("#c-subject_lv option:selected").text().replace(/\s|&nbsp;|├|└/g, '');
                    }
                    if(e.type=="click"&&$("#c-name").val()==""){
                        $("#c-name").val(nianji+xueyuan+xueqi+subject+"第"+qishu+"期"+"1班");
                    }else if(e.type=="dblclick"){
                        $("#c-name").val(nianji+xueyuan+xueqi+subject+"第"+qishu+"期"+"1班");
                    }
                });
            });
            var row = JSON.parse($('#row').val());
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/add.js';
            document.head.appendChild(script);
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                url: function (value, row, index) {
                    return '<div class="input-group input-group-sm" style="width:250px;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
                },
                ip: function (value, row, index) {
                    return '<a class="btn btn-xs btn-ip bg-success"><i class="fa fa-map-marker"></i> ' + value + '</a>';
                },
                status: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="classes/classes/status" data-confirm="确认切换状态？" data-id="' + row.id + '"><i class="fa ' + (row.status == 0 ||row.status == null ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
                changeStatus: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="classes/classes/classes/classes/changeStatus" data-confirm="确认切换状态？" data-id="' + row.id + '"><i class="fa ' + (row.status === 0 ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            },
            events: {//绑定事件的方法
                ip: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-ip': function (e, value, row, index) {
                        e.stopPropagation();
                        var container = $("#table").data("bootstrap.table").$container;
                        var options = $("#table").bootstrapTable('getOptions');
                        //这里我们手动将数据填充到表单然后提交
                        $("form.form-commonsearch [name='ip']", container).val(value);
                        $("form.form-commonsearch", container).trigger('submit');
                        Toastr.info("执行了自定义搜索操作");
                    }
                },
            }
        }
    };
    return Controller;
});